
#import "FWBlockedAppListController.h"

#define kBlockedApplicationsFileName @"/listb.txt"
#define kBlockedApplicationsFilePath [[NSBundle mainBundle].resourcePath stringByAppendingString:kBlockedApplicationsFileName]

@interface FWBlockedAppListController () <NSTableViewDataSource>

@property (weak) IBOutlet NSTableView *tableView;

@property (nonatomic, strong) NSMutableArray *saveList;
@property (nonatomic, strong) NSMutableArray *list;

@property (weak) IBOutlet AppController *applicationController;

@end

@implementation FWBlockedAppListController

- (id)init
{
    self = [super init];
    if (self) {
        self.saveList = [NSMutableArray new];
        self.list = [NSMutableArray new];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveRecords:) name:@"Save" object:nil];
    [super awakeFromNib];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupSavedRecords:) name:@"Load" object:nil];

    //[self setupSavedRecords];
}

- (void)setupSavedRecords:(NSNotification *) notification

{
    NSLog(@"\n\n\n\n\n");
    
    NSArray *recordsArray = [[NSArray alloc] initWithContentsOfFile:kBlockedApplicationsFilePath];
    
    for (NSString *applicationName in recordsArray) {
        BlockController *blockedApplicationController = [BlockController new];
        blockedApplicationController.name = applicationName;
        [blockedApplicationController sendLoad];
        
        [self.list addObject:blockedApplicationController];
        [self.saveList addObject:applicationName];
       // [self.saveList writeToFile:kBlockedApplicationsFilePath atomically:YES];
        
       
        
    }
    
    [self reload];
}


//- (void)saveRecords
//{
//    [self.saveList writeToFile:kBlockedApplicationsFilePath atomically:YES];
//    
//    NSLog(@"list saved %ld",(unsigned long)[self.saveList count]);
//}

- (void)saveRecords:(NSNotification *) notification
{
    [self.saveList writeToFile:kBlockedApplicationsFilePath atomically:YES];
    
   NSLog(@"list saved %ld",(unsigned long)[self.saveList count]);
}
- (void)reload
{
    [self.tableView reloadData];
}

-(IBAction)addFromFinderButtonDidTapped:(id)sender
{
     BlockController *blockedApplicationController = [BlockController new];
    [blockedApplicationController sendPath];
    if(![blockedApplicationController.name isEqual:@"YODA"]){
    [self.list addObject:blockedApplicationController];
    [self.saveList addObject:blockedApplicationController.name];
    }
    [self reload];
}

- (IBAction)addRecordButtonDidTapped:(id)sender
{
    [self.applicationController updateModel];
    
    BlockController *blockedApplicationController = [BlockController new];
    
    NSString *applicationName = self.applicationController.path;
    
    blockedApplicationController.name = applicationName;
    
    [self.list addObject:blockedApplicationController];
    [self.saveList addObject:applicationName];
    
    [self reload];
}

- (IBAction)removeRecordButtonDidTapped:(id)sender
{
    NSInteger currentRow = self.tableView.selectedRow;
    
    if (currentRow > -1 && currentRow != NSNotFound) {
        
        BlockController *blockedApplicationController = self.list[currentRow];
        
        [blockedApplicationController delPath:blockedApplicationController.name];
        
        [self.list removeObjectAtIndex:currentRow];
        [self.saveList removeObjectAtIndex:currentRow];
        
        [self reload];
    }
}


#pragma mark -
#pragma mark - NSTableViewDataSource

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return self.list.count;
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    BlockController *blockedApplicationController = self.list[row];
    
    NSString *cellIdentifier = tableColumn.identifier;
    
    id aObject = nil;
    
    @try {
        aObject = [blockedApplicationController valueForKey:cellIdentifier];
    }
    @catch (NSException *exception) {
    }
    
    return aObject;
}

@end
